package handlers.voicedcommandhandlers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;

import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
import com.l2jserver.gameserver.instancemanager.HellboundManager;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;

public class HellboundVoiced implements IVoicedCommandHandler
{
    private static final String[] VOICED_COMMANDS = { "trust","countslaves" };

    public boolean useVoicedCommand(String command, L2PcInstance activeChar, String target)
    {
        if (command.startsWith("trust"))
        {
            HellboundManager.getInstance().checkHellboundLevel();
            boolean _isOpen = HellboundManager.getInstance().checkIsOpen();
            int hellboundLevel = HellboundManager.getInstance().getLevel();
            int hellboundTrust = HellboundManager.getInstance().getTrust();
            activeChar.sendMessage("### Hellbound Island Info ###");
            if (_isOpen == true)
                activeChar.sendMessage("Stato: Accessibile");
            if (_isOpen == false)
                activeChar.sendMessage("Status: Bloccata");
            activeChar.sendMessage("Punti trust: " + hellboundTrust);
            activeChar.sendMessage("Livello: " + hellboundLevel);
        }
        else if(command.startsWith("countslaves")){
            activeChar.sendMessage("### Hellbound Native Quarry Slaves ###");
            activeChar.sendMessage("Schiavi liberi: "+loadGlobalQuestVar("rescued_Slaves", "Native_Quarry_Slaves"));
        }
        return true;
    }

    public String[] getVoicedCommandList()
    {
        return VOICED_COMMANDS;
    }
   
    public static void main(String[] args)
    {
        new HellboundVoiced();
    }
   
    private String loadGlobalQuestVar(String var, String name)
    {
        String result = "";
        Connection con = null;
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement;
            statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
            statement.setString(1, name);
            statement.setString(2, var);
            ResultSet rs = statement.executeQuery();
            if (rs.first())
                result = rs.getString(1);
            rs.close();
            statement.close();
        }
        catch (Exception e)
        {
            _log.log(Level.WARNING, "could not load global quest variable:", e);
        }
        finally
        {
            try
            {
                con.close();
            }
            catch (Exception e)
            {
            }
        }
        return result==null?"0":result;
    }
}